#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int longestSubstring(string s, int k) {
    auto dfs = [&](auto&& dfs, int l, int r)->int {
        vector<int> cnt(26);
        for (int i = l; i <= r; i++)   cnt[s[i] - 'a']++;

        char split = 0;
        for (int i = 0; i < 26; i++)
            if (cnt[i] > 0 && cnt[i] < k)
            {
                split = i + 'a';
                break;
            }
        if (split == 0)  return r - l + 1;

        int i = l, ret = 0;
        while (i <= r)
        {
            while (i <= r && s[i] == split)
                i++;
            if (i > r)   break;
            int st = i;
            while (i <= r && s[i] != split)
                i++;

            int len = dfs(dfs, st, i - 1);
            ret = max(ret, len);
        }
        return ret;
    };

    return dfs(dfs, 0, s.size() - 1);
}

int main()
{
    longestSubstring("ababbc", 2);
    return 0;

}